/*
 * Copyright (C) 2015 Miquel Sas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package traderlib.trash;

import traderlib.core.data.db.*;
import java.util.ArrayList;
import java.util.List;
import traderlib.core.data.entity.FieldMap;
import traderlib.core.data.types.KeyPointer;

/**
 * A column list packs a list of columns, its column map, the primary key pointers and the order y key pointers if
 * applicable.
 *
 * @author Miquel Sas
 */
public class ColumnList {

	/**
	 * The field map to access columns by alias.
	 */
	private transient FieldMap columnMap = null;
	/**
	 * The list of columns.
	 */
	private final List<Column> columns = new ArrayList<>();
	/**
	 * The list of persistent columns.
	 */
	private List<Column> persistentColumns;
	/**
	 * The list of primary key pointers.
	 */
	private List<KeyPointer> primaryKeyPointers = null;
	/**
	 * The list of persistent key pointers.
	 */
	private List<KeyPointer> persistentKeyPointers = null;

	/**
	 * Default constructor.
	 */
	public ColumnList() {
		super();
	}

	/**
	 * Clear this column list.
	 */
	public void clear() {
		columns.clear();
		clearColumnMap();
	}

	/**
	 * Returns this column listt size.
	 *
	 * @return The number of columns.
	 */
	public int size() {
		return columns.size();
	}

	/**
	 * Clear the column map, necessary when changing columns.
	 */
	public void clearColumnMap() {
		FieldMap.removeMap(columnMap);
		columnMap = null;
		primaryKeyPointers.clear();;
		primaryKeyPointers = null;
		persistentColumns.clear();
		persistentColumns = null;
		persistentKeyPointers.clear();
		persistentKeyPointers = null;
	}

	/**
	 * Returns this table column map.
	 *
	 * @return The column map.
	 */
	public FieldMap getColumnMap() {
		if (columnMap == null) {
			columnMap = new FieldMap(columns);
		}
		return columnMap;
	}

	/**
	 * Add a column to the column list.
	 *
	 * @param column The column to add.
	 */
	public void addColumn(Column column) {
		columns.add(column);
		clearColumnMap();
	}

	/**
	 * Returns the number of columns in this table.
	 *
	 * @return The number of columns.
	 */
	public int getColumnCount() {
		return columns.size();
	}

	/**
	 * Returns the column in the given index.
	 *
	 * @param index The index of the column.
	 * @return The column
	 */
	public Column getColumn(int index) {
		return columns.get(index);
	}

	/**
	 * Get a column by alias.
	 *
	 * @return The column or null if not found.
	 * @param alias The column alias.
	 */
	public Column getColumn(String alias) {
		int index = getColumnIndex(alias);
		return (index == -1 ? null : getColumn(index));
	}

	/**
	 * Get a column index by alias.
	 *
	 * @return The column index or -1 if not found.
	 * @param alias The column alias.
	 */
	public int getColumnIndex(String alias) {
		return getColumnMap().getFieldIndex(alias);
	}

	/**
	 * Get a column index.
	 *
	 * @return The column index or -1 if not found.
	 * @param column The column.
	 */
	public int getColumnIndex(Column column) {
		return getColumnIndex(column.getAlias());
	}

	/**
	 * Returns the internal list of columns.
	 *
	 * @return
	 */
	public List<Column> getColumns() {
		return columns;
	}

	/**
	 * Returns the list of persistent columns.
	 *
	 * @return The list of persistent columns.
	 */
	public List<Column> getPersistentColumns() {
		if (persistentColumns == null) {
			persistentColumns = new ArrayList<>();
			for (Column column : columns) {
				if (column.isPersistent()) {
					persistentColumns.add(column);
				}
			}
		}
		return persistentColumns;
	}

	/**
	 * Returns the list of persistent key pointers.
	 *
	 * @return The list of persistent key pointers.
	 */
	public List<KeyPointer> getPersistentKeyPointers() {
		if (persistentKeyPointers == null) {
			persistentKeyPointers = new ArrayList<>();
			for (int i = 0; i < columns.size(); i++) {
				Column column = columns.get(i);
				if (column.isPersistent()) {
					persistentKeyPointers.add(new KeyPointer(i));
				}
			}
		}
		return persistentKeyPointers;
	}

	/**
	 * Removes all the columns in this table.
	 *
	 * @return The removed columns
	 */
	public List<Column> removeAllColumns() {
		clearColumnMap();
		List<Column> removedColumns = new ArrayList<>();
		removedColumns.addAll(columns);
		columns.clear();
		return removedColumns;
	}

	/**
	 * Remove the column at the given index.
	 *
	 * @param index The index of the column
	 * @return The removed column or null.
	 */
	public Column removeColumn(int index) {
		clearColumnMap();
		return columns.remove(index);
	}

	/**
	 * Remove the with the given alias.
	 *
	 * @param alias The alias of the column
	 * @return The removed column or null.
	 */
	public Column removeColumn(String alias) {
		int index = getColumnIndex(alias);
		if (index < 0) {
			return null;
		}
		return removeColumn(index);
	}

	/**
	 * Returns the list of primary key columns.
	 *
	 * @return The list of primary key columns.
	 */
	public List<Column> getPrimaryKeyColumns() {
		List<Column> primaryKeyColumns = new ArrayList<>();
		for (Column column : columns) {
			if (column.isPrimaryKey()) {
				primaryKeyColumns.add(column);
			}
		}
		return primaryKeyColumns;
	}

	/**
	 * Returns the list of primary key pointers.
	 *
	 * @return The list of primary key pointers.
	 */
	public List<KeyPointer> getPrimaryKeyPointers() {
		if (primaryKeyPointers == null) {
			primaryKeyPointers = new ArrayList<>();
			for (int i = 0; i < columns.size(); i++) {
				Column column = columns.get(i);
				if (column.isPrimaryKey()) {
					primaryKeyPointers.add(new KeyPointer(i, true));
				}
			}
			if (primaryKeyPointers.isEmpty()) {
				for (int i = 0; i < columns.size(); i++) {
					primaryKeyPointers.add(new KeyPointer(i, true));
				}
			}
		}
		return primaryKeyPointers;
	}

}
